package org.judking.CarKeeper.activity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.judking.CarKeeper.R;
import org.judking.CarKeeper.command.BaseObdCommand;
import org.judking.CarKeeper.command.PddObdCommand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BasicDataItemAdapter extends BaseAdapter {
	private LinkedHashMap<String, PddObdCommand> cmdMap;
	private List<PddObdCommand> cmdList;
	private Context context;
	
	public BasicDataItemAdapter(Context context)			{
		cmdMap = new LinkedHashMap<String, PddObdCommand>();
		cmdList = new ArrayList<PddObdCommand>(cmdMap.values());
		this.context = context;
	}
	
	public BasicDataItemAdapter(List<BaseObdCommand> baseObdCommands, Context context)		{
		cmdMap = assembleCmdMap(baseObdCommands);
		cmdList = new ArrayList<PddObdCommand>(cmdMap.values());
		this.context = context;
	}
	
	public synchronized void setCmdsAndRefresh(List<BaseObdCommand> baseObdCommands) {
		this.cmdMap = assembleCmdMap(baseObdCommands);
		cmdList = new ArrayList<PddObdCommand>(cmdMap.values());
		notifyDataSetChanged();
	}
	
	private LinkedHashMap<String, PddObdCommand> assembleCmdMap(List<BaseObdCommand> baseObdCommands)		{
		LinkedHashMap<String, PddObdCommand> orderedCmdMap = new LinkedHashMap<String, PddObdCommand>();
		
		Map<String, PddObdCommand> tempPddMap = filterPddCmd(baseObdCommands);
		//get common data and store first in orderedCmdMap
		PddObdCommand tmpPdd = null;
		if((tmpPdd = tempPddMap.get("010d")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("010d");
		}
		if((tmpPdd = tempPddMap.get("010c")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("010c");
		}
		if((tmpPdd = tempPddMap.get("0105")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("0105");
		}
		if((tmpPdd = tempPddMap.get("010f")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("010f");
		}
		if((tmpPdd = tempPddMap.get("011f")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("011f");
		}
		if((tmpPdd = tempPddMap.get("012f")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("012f");
		}
		if((tmpPdd = tempPddMap.get("0133")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("0133");
		}
		if((tmpPdd = tempPddMap.get("0146")) != null)		{
			orderedCmdMap.put(tmpPdd.genCmd(), tmpPdd);
			tempPddMap.remove("0146");
		}
		//add the other pdd data in orderedCmdMap
		orderedCmdMap.putAll(tempPddMap);
		
		return orderedCmdMap;
	}
	
	private Map<String, PddObdCommand> filterPddCmd(List<BaseObdCommand> baseObdCommands)			{
		Map<String, PddObdCommand> map = cmdMap != null ? cmdMap : new LinkedHashMap<String, PddObdCommand>();
		for(BaseObdCommand cmd : baseObdCommands)		{
			if(cmd instanceof PddObdCommand)		{
				PddObdCommand tmp = (PddObdCommand)cmd;
				map.put(tmp.genCmd(), tmp);
			}
		}
		return map;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cmdList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return cmdList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int pos, View view, ViewGroup group) {
		// TODO Auto-generated method stub
		View v = view;
		if(v == null)			{
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.item_basic_data_list, null);
		}
		
		TextView dataName = (TextView)v.findViewById(R.id.tv_data_name);
		TextView dataContent = (TextView)v.findViewById(R.id.tv_data_content);
		PddObdCommand cmd = cmdList.get(pos);
		
		dataName.setText(cmd.getName());
		dataContent.setText(cmd.getFmtResult());
		
		v.setOnClickListener(new BasicDataItemOnClickListener(cmd));
		
		return v;
	}

}
